package com.wondream.plumelog.lite.samples.utils;

import com.plumelog.core.TraceId;
import com.plumelog.core.util.IdWorker;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * PlumeLog请求打点Utils
 *
 */
public class PlumeLogUtils {
    static IdWorker worker = new IdWorker(1, 1, 1);

    /**
     * 请求打点（为每次请求加上链路码）
     *
     * @param httpServletRequest
     */
    public static void setTraceIdInRequest(HttpServletRequest httpServletRequest) {
        String traceId = httpServletRequest.getHeader("TraceId");
        if (StringUtils.isEmpty(traceId)) {
            TraceId.logTraceID.set(String.valueOf(worker.nextId()));
        } else {
            TraceId.logTraceID.set(traceId);
        }
    }
}
